package com.cyt.web.controller.work.wb;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.work.wb.model.po.*;
import com.cyt.work.wb.model.vo.*;
import com.cyt.work.wb.service.*;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 维保模块-设备表控制层
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:26:43
 */
@RestController
public class WbDevicesController extends BaseController {

    @Resource
    private WbDevicesService wbDevicesService;

    @Resource
    private WbDevicesTypeService wbDevicesTypeService;

    @Resource
    private WbContractService wbContractService;

    @Resource
    private WbServicerService wbServicerService;

    @Resource
    private WxWbRoomService wxWbRoomService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    /**
     * 统一跳转
     *
     * @return Model
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @RequestMapping("/work/wb/wbdevices/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId,String wbcontractId) {
        ModelAndView view = new ModelAndView("/work/wb/wbdevices/"+pageName);
        view.addObject("applyId", applyId);
        view.addObject("wbcontractId", wbcontractId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param wbDevicesVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    @BaseLog(module = "维保模块", classz = "设备表", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wbdevices/page", method = RequestMethod.POST)
    public ReturnResult page(WbDevicesVo wbDevicesVo, int page, int limit) {
        return wbDevicesService.page(wbDevicesVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    @BaseLog(module = "维保模块", classz = "设备表", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wbdevices/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id,String applyId,String deviceTypeId,String contractId) {
        ModelAndView view = new ModelAndView("/work/wb/wbdevices/wbDevicesEdit");
        WbDevices wbDevices=new WbDevices();
        if (StringUtils.isNotBlank(id)) {
            wbDevices=wbDevicesService.findById(id);
        }
        if(StringUtils.isNotBlank(deviceTypeId)){
            WbDevicesType wbDevicesType=wbDevicesTypeService.selectByPrimaryKey(deviceTypeId);
            wbDevices.setDeviceTypeId(wbDevicesType.getId());
            wbDevices.setDeviceTypeName(wbDevicesType.getName());
        }
        if(StringUtils.isNotBlank(contractId)){
            WbContract wbContract=wbContractService.selectByPrimaryKey(contractId);
            wbDevices.setContractId(wbContract.getId());
            wbDevices.setContractName(wbContract.getName());
        }

        wbDevices.setApplyId(applyId);
        view.addObject(wbDevices);
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wbDevicesVo 接受参数对象
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    @BaseLog(module = "维保模块", classz = "设备表", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wbdevices/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WbDevicesVo wbDevicesVo) {
        return wbDevicesService.addOrUpdate(request,wbDevicesVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wbDevicesVo 接受参数对象
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    @BaseLog(module = "维保模块", classz = "设备表", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wbdevices/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WbDevicesVo wbDevicesVo) {
        return wbDevicesService.remove(wbDevicesVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wbDevicesVo 接受参数对象
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:43
     */
    @BaseLog(module = "维保模块", classz = "设备表", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wbdevices/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WbDevicesVo wbDevicesVo) {
        return wbDevicesService.delete(wbDevicesVo, acceptCurrentUser(request));
    }

    /**
     * 导出房间模板
     * @param response
     * @author hanjunW
     * @date 2019/9/3 13:43
     */
    @RequestMapping(value = "/work/wb/wbdevices/export")
    public void exportExcel(HttpServletResponse response,String applyId) throws IOException {
        List<WbDevicesVo> list = new ArrayList<>();

        // 设置联动模板(设备分类)
        List<WbDevicesType> listTypes=wbDevicesTypeService.listExample(new WbDevicesTypeVo(applyId));
        String[] provinces =new String[listTypes.size()];
        for(int i=0;i<listTypes.size();i++){
            provinces[i]=listTypes.get(i).getExcelModel();
        }
        //设置联动模板（合同）
        List<WbContract> listContract=wbContractService.listExample(new WbContractVo(applyId));
        String[] contracts=new String[listContract.size()];
        for(int i=0;i<listContract.size();i++){
            contracts[i]=listContract.get(i).getName();
        }
        //设置联动模板（供应商）
        List<WbServicer> listWbServicer=wbServicerService.listExample(new WbServicerVo(applyId));
        String[] listServicer=new String[listWbServicer.size()];
        for(int i=0;i<listWbServicer.size();i++){
            listServicer[i]=listWbServicer.get(i).getName();
        }
        //设置联动模板（所在房间）
        List<WxWbRoomVo> WbRooms=wxWbRoomService.listExample(new WxWbRoomVo(applyId));
        String[] listWbRoom=new String[WbRooms.size()];
        for(int i=0;i<WbRooms.size();i++){
            listWbRoom[i]=WbRooms.get(i).getBulidExcelModel()+"-"+WbRooms.get(i).getRoomName();
        }
        EasyPoiUtil.exportExcelModelDevicesType(list,"设备信息.xls",provinces,contracts,listServicer,listWbRoom,response,WbDevicesExportVo.class);
    }

    /**
     * 导入房间信息
     * @param file
     * @param request
     * @author hanjun
     * @date 2019/9/3 13:33
     */
    @RequestMapping(value = "/work/wb/wbdevices/importExcel", method = RequestMethod.POST)
    public ReturnResult importExcel(MultipartFile file, HttpServletRequest request, String applyId) {
        return wbDevicesService.importExcel(request,file, acceptCurrentUser(request),applyId);
    }
}